package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter_zqs {
    private static final String DB_NAME_ZQS = "book_zqs.db";
    private static final String DB_TABLE_ZQS = "peopleinfo_zqs";
    private static final int DB_VERSION_ZQS = 1;

    public static final String KEY_ID_ZQS = "_id";
    public static final String NAME_ZQS = "name_zqs";
    public static final String TEXT_ZQS = "text_zqs";
    public static final String TABLE_ZQS = "table_zqs";
    public static final String FIRSTTIME_ZQS = "firsttime_zqs";
    public static final String FINALLYTIME_ZQS = "finallytime_zqs";

    private SQLiteDatabase db_zqs;
    private final Context context_zqs;
    private DBOpenHelper_zqs dbOpenHelper_zqs;

    public DBAdapter_zqs(Context _context) {
        context_zqs = _context;
    }

    public void close() {
        if (db_zqs != null){
            db_zqs.close();
            db_zqs = null;
        }
    }


    public void open() throws SQLiteException {
        dbOpenHelper_zqs = new DBOpenHelper_zqs(context_zqs, DB_NAME_ZQS, null, DB_VERSION_ZQS);
        try {
            db_zqs = dbOpenHelper_zqs.getWritableDatabase();
        }
        catch (SQLiteException ex) {
            db_zqs = dbOpenHelper_zqs.getReadableDatabase();
        }
    }



    public long insert(com.example.myapplication.People_zqs people_zqs) {
        ContentValues newValues_zqs = new ContentValues();
        newValues_zqs.put(NAME_ZQS, people_zqs.Name_zqs);
        newValues_zqs.put(TABLE_ZQS, people_zqs.Table_zqs);
        newValues_zqs.put(TEXT_ZQS, people_zqs.Text_zqs);
        newValues_zqs.put(FIRSTTIME_ZQS, people_zqs.Firsttime_zqs);
        newValues_zqs.put(FINALLYTIME_ZQS, people_zqs.Finallytime_zqs);
        return db_zqs.insert(DB_TABLE_ZQS, null, newValues_zqs);
    }


    public com.example.myapplication.People_zqs[] queryAllData() {
        Cursor results =  db_zqs.query(DB_TABLE_ZQS, new String[] { KEY_ID_ZQS, NAME_ZQS, TABLE_ZQS, TEXT_ZQS,
                FIRSTTIME_ZQS,FINALLYTIME_ZQS},
                null, null, null, null, "finallytime_zqs asc");
        return ConvertToPeople(results);
    }

    public com.example.myapplication.People_zqs[] queryOneData(long id) {
        Cursor results =  db_zqs.query(DB_TABLE_ZQS, new String[] { KEY_ID_ZQS, NAME_ZQS, TEXT_ZQS,TABLE_ZQS,
                FINALLYTIME_ZQS,FIRSTTIME_ZQS},
                KEY_ID_ZQS+ "=" + id, null, null, null, null);
        return ConvertToPeople(results);
    }

    private com.example.myapplication.People_zqs[] ConvertToPeople(Cursor cursor){
        int resultCounts = cursor.getCount();
        if (resultCounts == 0 || !cursor.moveToFirst()){
            return null;
        }
        com.example.myapplication.People_zqs[] peoples = new com.example.myapplication.People_zqs[resultCounts];
        for (int i = 0 ; i<resultCounts; i++){
            peoples[i] = new com.example.myapplication.People_zqs();
            peoples[i].ID = cursor.getInt(0);
            peoples[i].Name_zqs = cursor.getString(cursor.getColumnIndex(NAME_ZQS));
            peoples[i].Text_zqs = cursor.getString(cursor.getColumnIndex(TEXT_ZQS));
            peoples[i].Table_zqs = cursor.getString(cursor.getColumnIndex(TABLE_ZQS));
            peoples[i].Firsttime_zqs = cursor.getString(cursor.getColumnIndex(FIRSTTIME_ZQS));
            peoples[i].Finallytime_zqs = cursor.getString(cursor.getColumnIndex(FINALLYTIME_ZQS));


            cursor.moveToNext();
        }
        return peoples;
    }

    public long deleteAllData() {
        return db_zqs.delete(DB_TABLE_ZQS, null, null);
    }

    public long deleteOneData(long id) {
        return db_zqs.delete(DB_TABLE_ZQS,  KEY_ID_ZQS + "=" + id, null);
    }


    public long updateOneData(long id , com.example.myapplication.People_zqs people_zqs){
        ContentValues updateValues = new ContentValues();
        updateValues.put(NAME_ZQS, people_zqs.Name_zqs);
        updateValues.put(TEXT_ZQS, people_zqs.Text_zqs);
        updateValues.put(TABLE_ZQS, people_zqs.Table_zqs);
        updateValues.put(FINALLYTIME_ZQS, people_zqs.Finallytime_zqs);

        return db_zqs.update(DB_TABLE_ZQS, updateValues,  KEY_ID_ZQS + "=" + id, null);
    }

    private static class DBOpenHelper_zqs extends SQLiteOpenHelper
    {

        public DBOpenHelper_zqs(Context context, String name, CursorFactory factory, int version)
        {
            super(context, name, factory, version);
        }

        private static final String DB_CREATE = "create table " +
                DB_TABLE_ZQS + " (" + KEY_ID_ZQS+ " integer primary key autoincrement, " +
                NAME_ZQS+ " text not null, " + TABLE_ZQS+ " integer,"  + TEXT_ZQS+ " integer,"
                + FIRSTTIME_ZQS+ " integer," + FINALLYTIME_ZQS+ " integer);";

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
            _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE_ZQS);
            onCreate(_db);
        }
    }
}
